package service;

import java.sql.SQLException;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.IAtom;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.ehcache.CacheKit;
import com.jfinal.plugin.ehcache.IDataLoader;

import config.model.Changepeople;

/**
 * 变更申请相关处理服务类
 * @author 陈凌志
 * 
 */

public class ChangeService {

	/**
	 * 此方法用于添加人员变更申请
	 * @param cp 人员变更申请信息
	 * @return boolean类型，true表示成功，false表示失败
	 * 
	 */
	
	public boolean addChangePeople(Changepeople cp) {
		CacheKit.removeAll("change");
		return cp.set("check", "未审核").save();
	}
	
	/**
	 * 此方法用于以负责人查询项目的人员变更申请
	 * @param personsResponsibleName 负责人姓名
	 * @return List<Record>类型，为查询到的人员变更信息
	 * 
	 */
	
	public List<Record> queryChangePeople(String personsResponsibleName) {
		List<Record> changePeople = CacheKit.get("change", personsResponsibleName+"changePeople", new IDataLoader(){
		       public Object load() {
		           return Db.find("select * from changePeople where personsResponsibleName = ? ",personsResponsibleName);
		    }});
		return changePeople;
	}
	
	/**
	 * 此方法用于查询人员变更申请
	 * @param pageNumber 当前页码
	 * @return Page<Record>类型，为查询出的所有项目信息
	 * 
	 */
	public Page<Record> queryChangePeople(int pageNumber) {
		Page<Record> change = CacheKit.get("change", "admin", new IDataLoader(){
		       public Object load() {
		           return Db.paginate(pageNumber, 5, 
		        		   "select a.projectId,a.type,a.before,a.after,a.reason,b.year,b.levels,b.name"
		        		   ,"from changepeople a join project b on a.projectId=b.projectId where a.check = ?","未审核");
		    }});
		return change;
	}
	
	/**
	 * 此方法用于项目的人员变更申请审核
	 * @param projectId 项目ID
	 * @param 审核结果（通过或拒绝）
	 * @param 类型（学生或教师）
	 * @return boolean类型，成功返回true，否则返回false
	 * 
	 */
	public boolean changPeople(String projectId,String check,String type) {
		boolean succeed = false;
		if(type.equals("教师变更申请") ) {
			succeed = Db.tx(new IAtom(){
			    public boolean run() throws SQLException {
			    	int count1 = Db.update("update changepeople set `check` = ? where projectId = ?", check,projectId);
			    	int count = Db.update("UPDATE project A,changepeople B SET A.teacherName =B.after,A.title = B.title" + 
							"WHERE B.projectId=A.projectId AND B.`check` = ?","通过");
			    	int count2 = Db.update("UPDATE changepeople SET `check` = ? WHERE projectId = ? AND `check` = ?","通过且已更新",projectId,"通过");
			    	return count == 1 && count2 == 1 && count1 == 1;
			    }});
			return succeed;
		}else if(type.equals("团队成员变更申请")) {
			succeed = Db.tx(new IAtom(){
			    public boolean run() throws SQLException {
			    	int count1 = Db.update("update changepeople set `check` = ? where projectId = ?", check,projectId);
			       int count = Db.update("UPDATE project A,changepeople B SET A.otherPeople = B.`after`" + 
							"WHERE B.projectId=A.projectId AND B.`check` = ?","通过");
			       int count2 = Db.update("UPDATE changepeople SET `check` = ? WHERE projectId = ? AND `check` = ?","通过且已更新",projectId,"通过");
			       return count == 1 && count2 == 1 && count1 == 1;
			    }});
			return succeed;
		}
		CacheKit.removeAll("change");
		return succeed;
	}
}
